package com.demo.common;

import javax.servlet.http.HttpServletRequest;

/**
 * 网络工具类
 * <p>1.从HttpServletRequest取得用户登录IP地址</p>
 * 
 * @author ryuu.kk
 *
 */
public final class NetUtils {

	//private class
	private NetUtils() {}
	
	/**
	 * 取得登陆用户的IP地址
	 * @param request HttpServletRequest
	 * @return String ip地址
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if ("0:0:0:0:0:0:0:1".equals(ip)) {
			ip = "127.0.0.1";
		}
		return ip;
	}
	
	/**
	 * 取得登陆用户的IP地址
	 * @param request HttpServletRequest
	 * @return String ip地址
	 */
	public static String getRegion(HttpServletRequest request) {
		String ip = getIpAddr(request);
		
		String region = "";
		
		region += ip;
		
		return ip;
	}
}
